﻿using SuperSocket.ProtoBase;
using Google.Protobuf;

namespace Core;

public abstract class BaseCommand : IKeyedPackageInfo<CommandType>
{
    public static readonly Dictionary<CommandType, Type> Commands = new();

    static BaseCommand()
    {
        var typeArray = typeof(BaseCommand).Assembly.GetTypes();

        foreach (var classType in typeArray)
        {
            var command = classType.GetCustomAttributes(true)
                .OfType<CommandKeyAttribute>()
                .FirstOrDefault();

            if (command == null)
                continue;

            Commands.TryAdd(command.Key, classType);
        }
    }

    public CommandType Key { get; set; }

    public virtual ReadOnlySpan<byte> EncodeBody()
    {
        return ((IMessage)this).ToByteArray();
    }
}

[CommandKey(CommandType.Login)]
public sealed partial class LoginRequest : BaseCommand
{
}

[CommandKey(CommandType.LoginReply)]
public sealed partial class LoginReply : BaseCommand
{
}

[CommandKey(CommandType.AddOrder)]
public sealed partial class AddOrderRequest : BaseCommand
{
}

[CommandKey(CommandType.AddOrderReply)]
public sealed partial class AddOrderReply : BaseCommand
{
}

[CommandKey(CommandType.HeartBeat)]
public sealed partial class HeartbeatRequest : BaseCommand
{
}

[CommandKey(CommandType.HeartBeatReply)]
public sealed partial class HeartbeatReply : BaseCommand
{
}